package com.weipai;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Administrator on 2017/11/18 0018.
 */
public class Consumers {

    private static  final String brok_url ="failover:(tcp://116.62.119.148:61616)";
    private static  final String quenue ="jiuxin";


    //消费
    public   void    Consouremerqenue(){
        final ServerHttp httprequest = new ServerHttp();
        final String url ="http://api.jiuxin.datacdn.cn/commonconsumer";
        ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, brok_url);
        // 通过工厂创建一个连接
        try {
            Connection connection = factory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.FALSE, Session.CLIENT_ACKNOWLEDGE);
            // 创建一个消息队列
            Destination destination = session.createQueue(quenue);
            // 创建消费者
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new MessageListener() {
                public void onMessage(Message message) {
                    try {
                        try {
                            Thread.sleep(1);
                            String body = ((TextMessage) message).getText();
                            System.out.println("receive message ：" + body);
                            Boolean flag =   httprequest.SendMessage(url,body);
                            Thread.sleep(1500);
                            if(!flag){

                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        message.acknowledge();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            });
            new CountDownLatch(1).await();

        } catch (JMSException e) {
            e.printStackTrace();
        }catch (InterruptedException e) {

        }finally {

        }


    }

}
